<template>

  <section class="search">
    <div class="searchHeader">
        <div class="close" @click="back()"></div>
        <div class="searchInput">
            <input type="text" placeholder="请输入宝贝名称" @change='setHistory()'><button></button>
        </div>
    </div>
    <div class="searchHistory">
        <h4>搜索历史</h4>
        <ul>
            <router-link to="/Screen"><li v-for="(item,index) in history" :key="index">{{item.title}}</li></router-link>
        </ul>
    </div>
    <div class="searchhot">
        <h4>热门搜索</h4>
        <ul>
           <li v-for="(item,index) in arr" :key="index" @click="fn(index)"><router-link :to="{path:'/Screen',query: {kwords: item.title}}">{{item.title}}</router-link></li>
        </ul>
    </div>
  </section>

</template>

<script>
import axios from "axios";
export default {
  name: "search",
  props: [],
  mounted() {},
  data() {
    return {
      txt: "",
      history: [],
      historyArr: [],
      arr:[],
      zi:[]
    };
  },
  created() {
    if(localStorage.historyList){
      this.history = JSON.parse(localStorage.historyList);
      for(var i=0;i<this.history.length;i++){
        this.zi.push(this.history[i].title)
      }
      console.log(this.history)
      this.historyArr = JSON.parse(localStorage.historyList);
    }
    
    axios.post("http://vueshop.glbuys.com/api/home/public/hotwords?token=1ec949a15fb709370f").then(res => {
        this.arr = res.data.data;
        
    });
  },
  methods: {
    back() {
      this.$router.back(-1);
    },
    fn(id) {
      for(var i = 0;i<this.arr.length;i++){
        if(i == id){
          // console.log(this.arr[i].title)
          if(this.zi.indexOf(this.arr[i].title) == -1){
              this.historyArr.push(this.arr[i])
              // console.log(this.arr[i])

              // console.log(this.history)
          }else{
            console.log(1)
          }
          
        }
      }
      localStorage.setItem("historyList", JSON.stringify(this.historyArr));
    }
  },
  computed: {}
};
</script>

<style scoped>
.search {
  width: 100%;
  height: 100%;
  background: #fff;
  font-family: sans-serif;
}
.searchHeader {
  width: 100%;
  height: 1.14rem;
  border-bottom: 0.01rem solid #efefef;
  display: flex;
}
.searchHeader .close {
  width: 0.68rem;
  margin: 0 0.1rem 0 0.14rem;
  background: url("../assets/img/close.png") no-repeat center;
  background-size: 0.37rem auto;
}
.searchHeader .searchInput {
  flex: 1;
  padding: 0.2rem 0 0.2rem 0;
  box-sizing: border-box;
}
.searchHeader .searchInput input {
  width: 80%;
  height: 100%;
  box-sizing: border-box;
  float: left;
  border-radius: 0.08rem 0 0 0.08rem;
  border: 0.01rem solid #b2b2b2;
  outline: none;
  padding-left: 0.2rem;
  font-size: 0.36rem;
}
.searchHeader .searchInput button {
  width: 14%;
  height: 100%;
  box-sizing: border-box;
  background: url("../assets/img/search.png") no-repeat center;
  background-size: 0.4rem auto;
  border: 0.01rem solid #b2b2b2;
  border-left: none;
  border-radius: 0 0.08rem 0.08rem 0;
  outline: none;
}
.searchHistory {
  width: 100%;
  padding-left: 0.2rem;
  box-sizing: border-box;
}
.searchHistory h4 {
  width: 100%;
  height: 0.6rem;
  line-height: 0.6rem;
  margin-top: 0.2rem;
  font-size: 0.28rem;
}
.searchHistory ul {
  overflow: hidden;
}
.searchHistory ul li {
  float: left;
  width: 28%;
  height: 0.46rem;
  text-align: center;
  line-height: 0.46rem;
  border: 0.01rem solid #f3f3f3;
  margin: 0.1rem 0 0.1rem 0.1rem;
  border-radius: 0.2rem;
  color: #717376;
}
.searchhot {
  width: 100%;
  padding-left: 0.2rem;
  box-sizing: border-box;
}
.searchhot h4 {
  width: 100%;
  height: 0.6rem;
  line-height: 0.6rem;
  margin-top: 0.2rem;
  font-size: 0.28rem;
}
.searchhot ul {
  overflow: hidden;
}
.searchhot ul li {
  float: left;
  width: 28%;
  height: 0.46rem;
  text-align: center;
  line-height: 0.46rem;
  border: 0.01rem solid #f3f3f3;
  margin: 0.1rem 0 0.1rem 0.1rem;
  border-radius: 0.2rem;
  color: #717376;
}
.searchhot li a{
  color: #717376;
}
</style>
